1
|
|
|
export const getUser = function(state) { |
2
|
|
|
return state.user; |
3
|
|
|
}; |
4
|
|
|
|
5
|
|
|
export const isLoggedIn = function(state) { |
6
|
|
|
return state.user !== undefined && state.user !== null; |
7
|
|
|
}; |
8
|
|
|
|
9
|
|
|
export const getConfig = function(state) { |
10
|
|
|
return function(key = null) { |
11
|
|
|
if (key === null) { |
12
|
|
|
return state.config; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
if (typeof key !== 'string') { |
16
|
|
|
console.log('Comments config key must be a string.'); |
|
|
|
|
17
|
|
|
return; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
return state.config[key]; |
21
|
|
|
} |
22
|
|
|
}; |
23
|
|
|
|
24
|
|
|
export const isMyComment = function (state) { |
25
|
|
|
return function(comment) { |
26
|
|
|
if (typeof state.user === 'object' && state.user !== null) { |
27
|
|
|
return state.user.id == comment.user_id; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return false; |
31
|
|
|
} |
32
|
|
|
}; |
33
|
|
|
|
34
|
|
|
export const getOrderBy = function(state) { |
35
|
|
|
return state.orderBy; |
36
|
|
|
}; |
37
|
|
|
|
38
|
|
|
export const getComments = function(state) { |
39
|
|
|
return state.comments; |
40
|
|
|
}; |
41
|
|
|
|
42
|
|
|
export const hasChildren = function(state) { |
43
|
|
|
return function(model, modelId, parentId = null) { |
44
|
|
|
let result = state.comments.filter(function(comment) { |
45
|
|
|
// Has to be weak typed, fucking dynamic types |
46
|
|
|
return comment.model == model |
47
|
|
|
&& comment.foreign_key == modelId |
48
|
|
|
&& comment.parent_id == parentId; |
49
|
|
|
}); |
50
|
|
|
|
51
|
|
|
return result.length > 0; |
52
|
|
|
} |
53
|
|
|
}; |
54
|
|
|
|
55
|
|
|
export const hasMore = function(state) { |
56
|
|
|
return function(model, modelId, parentId = null) { |
57
|
|
|
let key = model + modelId + parentId; |
58
|
|
|
let pagination = Object.assign({}, state.pagination); |
59
|
|
|
|
60
|
|
|
if (pagination[key] !== undefined) { |
61
|
|
|
return pagination[key].nextPage; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
}; |
67
|
|
|
|
68
|
|
|
export const getCommentsForModel = function(state) { |
69
|
|
|
return function(model, modelId, parentId = null) { |
70
|
|
|
let comments = state.comments.filter(function(comment) { |
71
|
|
|
// Has to be weak typed, fucking dynamic types |
72
|
|
|
return comment.model == model |
73
|
|
|
&& comment.foreign_key == modelId |
74
|
|
|
&& comment.parent_id == parentId; |
75
|
|
|
}); |
76
|
|
|
|
77
|
|
|
comments.sort((a, b) => { |
78
|
|
|
switch (state.orderBy) { |
79
|
|
|
case 'newest': |
80
|
|
|
return a.created < b.created; |
81
|
|
|
case 'oldest': |
82
|
|
|
return a.created > b.created; |
83
|
|
|
default: |
84
|
|
|
return a.created < b.created; |
85
|
|
|
} |
86
|
|
|
}); |
87
|
|
|
|
88
|
|
|
return comments; |
89
|
|
|
} |
90
|
|
|
}; |
91
|
|
|
|
92
|
|
|
export const getPagination = function(state) { |
93
|
|
|
return function (model, modelId, parentId = null) { |
94
|
|
|
let key = model + modelId + parentId; |
95
|
|
|
|
96
|
|
|
if (state.pagination[key] !== undefined) { |
97
|
|
|
return state.pagination[key]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return { |
101
|
|
|
page: 1, |
102
|
|
|
nextPage: false, |
103
|
|
|
prevPage: false, |
104
|
|
|
pageCount: 1 |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|